home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / ANSIshellƒ / os_mac_consoleMW.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-18  |  4.2 KB  |  176 lines  |  [TEXT/MPCC]

  1. /************************************************************************/
  2. /*    Project...:    Standard ANSI-C Library                                    */
  3. /*    Name......:    console.c                                                */
  4. /*    Purpose...:    Stubs for console.c                                        */
  5. /*  Copyright.: ©Copyright 1994 by metrowerks inc. All rights reserved. */
  6. /************************************************************************/
  7.  
  8. #ifndef __CONSOLE__
  9. #include <console.h>
  10. #endif
  11.  
  12. /*
  13.  *    The following four functions provide the UI for the console package.
  14.  *    Users wishing to replace SIOUX with their own console package need
  15.  *    only provide the four functions below in a library.
  16.  */
  17.  
  18. /* I so wish... 04Jan95 e */
  19. #include <stdio.h>
  20. #include <signal.h>
  21. #include <errno.h>
  22. #include <unix.h>
  23. #include "os_mac.h"
  24. extern long gWaitTicksBG;
  25. extern long gWaitTicksFG;
  26.  
  27. Boolean pause_atexit = 1;
  28. Boolean end_of_input = 0; // 19Jun96 e
  29. unsigned char *console_title = "\peConsole";
  30.  
  31. /*
  32.  *    extern short InstallConsole(short fd);
  33.  *
  34.  *    Installs the Console package, this function will be called right
  35.  *    before any read or write to one of the standard streams.
  36.  *
  37.  *    short fd:        The stream which we are reading/writing to/from.
  38.  *    returns short:    0 no error occurred, anything else error.
  39.  */
  40.  
  41. static WindowPeek eConsole;
  42.  
  43. extern void (*__RemoveConsoleHandler__)(void);
  44.  
  45. short InstallConsole(short fd)
  46. {
  47.     #pragma unused( fd )
  48.     if ( eConsole == NULL )
  49.     {
  50.        eConsole = os_console_new( console_title );
  51.       _fcreator = CREATOR;
  52.       _ftype = 'BINA'; /* will be set to TEXT for text files */
  53. #if ( __MWERKS__ < 0x1100 )
  54.       __RemoveConsoleHandler__ = RemoveConsole;
  55. #endif
  56.     }
  57.     return 0;
  58. }
  59.  
  60. /*
  61.  *    extern void RemoveConsole(void);
  62.  *
  63.  *    Removes the console package.  It is called after all other streams
  64.  *    are closed and exit functions (installed by either atexit or _atexit)
  65.  *    have been called.  Since there is no way to recover from an error,
  66.  *    this function doesn't need to return any.
  67.  */
  68.  
  69. extern Boolean gDoQuit;
  70.  
  71. void RemoveConsole(void)
  72. {
  73. #ifdef DoSCRIPT
  74.   extern OSErr finish_AEDoScript( void );
  75.   if( finish_AEDoScript() != noErr )
  76.   {
  77. #endif
  78. #if pauseForQuitP
  79.     /*  pause for user acknowledgement  */
  80.     unsigned char buf[32];
  81.     if ( pause_atexit && !gDoQuit )
  82.     {    SetWTitle( (WindowPtr )eConsole, "\ppress «enter» to exit" );
  83.         while ( os_console_read_nohang( eConsole, buf, 32 ) == 0 ) /* wait */ ;
  84.     }
  85. #endif
  86. #ifdef DoSCRIPT
  87.   }
  88. #endif
  89. }
  90.  
  91. /*
  92.  *    extern long WriteCharsToConsole(char *buffer, long n);
  93.  *
  94.  *    Writes a stream of output to the Console window.  This function is
  95.  *    called by write.
  96.  *
  97.  *    char *buffer:    Pointer to the buffer to be written.
  98.  *    long n:            The length of the buffer to be written.
  99.  *    returns short:    Actual number of characters written to the stream,
  100.  *                    -1 if an error occurred.
  101.  */
  102.  
  103. long WriteCharsToConsole(char *buffer, long n)
  104. {
  105.     return os_console_write( eConsole, (unsigned char *)buffer, n );
  106. }
  107.  
  108. /*
  109.  *    extern long ReadCharsFromConsole(char *buffer, long n);
  110.  *
  111.  *    Reads from the Console into a buffer.  This function is called by
  112.  *    read.
  113.  *
  114.  *    char *buffer:    Pointer to the buffer which will recieve the input.
  115.  *    long n:            The maximum amount of characters to be read (size of
  116.  *                    buffer).
  117.  *    returns short:    Actual number of characters read from the stream,
  118.  *                    -1 if an error occurred.
  119.  */
  120.  
  121. long ReadCharsFromConsole(char *buffer, long n)
  122. { long result = 0;
  123. #if ReadHangsP
  124.   int idle = 0;
  125. #if EndOfInputP == 2
  126.   if (end_of_input)
  127.   { end_of_input = 0;
  128.     *buffer = 0;
  129.     return 0;
  130.   }
  131. #endif
  132.   while ((result = os_console_read_nohang( eConsole, (unsigned char *)buffer, n )) == 0)
  133.   {    if (interrupted)
  134.     { interrupted = 0;
  135.       raise( SIGINT );
  136.       errno = EINTR;
  137.       break;
  138.     }
  139. #if EndOfInputP == 2
  140.     if (end_of_input)
  141.     { end_of_input = 0;
  142.       *buffer = 0;
  143.       break;
  144.     }
  145. #endif
  146. #if EndOfInputP == 1
  147.     if (end_of_input) break;
  148. #endif
  149.     if ( idle < 8 )
  150.     { idle++;
  151.       gWaitTicksBG <<= 1;
  152.       gWaitTicksFG <<= 1;
  153.     }
  154.   }
  155. #if EndOfInputP == 1
  156.   end_of_input = 0;
  157. #endif
  158.   gWaitTicksBG >>= idle;
  159.   gWaitTicksFG >>= idle;
  160. #else
  161.   if ((result = os_console_read_nohang( eConsole, (unsigned char *)buffer, n )) == 0)
  162.   {     result = 0; /* EOF ? */
  163.   }
  164. #endif
  165.   return result;
  166. }
  167.  
  168. /* e */
  169.  
  170. long CharReadyConsoleP(void)
  171. {
  172.   return os_console_chars_ready( eConsole );
  173. }
  174.  
  175. /* end */
  176.